package com.sinopec.pi.scalaguide

object MatchCase {

    case class Person(name: String, age: Int)

    def main(args: Array[String]): Unit = {
        println(matchInt(3))

        println(matchAny("two"))
        println(matchAny("test"))
        println(matchAny(1))
        println(matchAny(6))

        // case class 匹配
        val alice = Person("Alice", 25)
        val bob = Person("Bob", 32)
        val charlie = Person("Charlie", 32)
        for (person <- List(alice, bob, charlie)) {
            person match {
                case Person("Alice", _) => println("Hi Alice!")
                case Person("Bob", age) if age > 30 => println("Hi Bob!")
                case Person(name, age) =>
                    println("Age: " + age + " year, name: " + name + "?")
            }
        }
    }

    def matchInt(x: Int): String = x match {
        case 1 => "one"
        case 2 => "two"
        case _ => "many"
    }

    def matchAny(x: Any): Any = x match {
        case 1 => "one"
        case "two" => 2
        case y: Int => "scala.Int"
        case _ => "many"
    }
}
